package com.lagou.homeworkzk.controller;

import com.lagou.homeworkzk.config.DynamicDatasourceHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@RestController
public class IndexController {

    @RequestMapping("/get")
    public String helloworld() {
        DataSource dataSource = DynamicDatasourceHolder.getDataSource();
        StringBuilder data=new StringBuilder("mysql version:");
        try {
            Connection conn = dataSource.getConnection();
            Statement statement = conn.createStatement();
            ResultSet rs = statement.executeQuery("SELECT VERSION()");
            while (rs.next()){
                data.append(rs.getString(1));
            }
            rs.close();
        } catch (SQLException throwables) {
            data.append("异常信息:");
            data.append(throwables.getMessage());
        }
        return data.toString();
    }
}
